/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.pojo.sqlServer.PPWmsTask;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.PPWmsTaskRepository;
import me.zhengjie.service.PPWmsTaskService;
import me.zhengjie.service.dto.PPWmsTaskDto;
import me.zhengjie.service.dto.PPWmsTaskQueryCriteria;
import me.zhengjie.service.mapper.PPWmsTaskMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-14
**/
@Service
@RequiredArgsConstructor
public class PPWmsTaskServiceImpl implements PPWmsTaskService {

    private final PPWmsTaskRepository ppWmsTaskRepository;
    private final PPWmsTaskMapper ppWmsTaskMapper;

    @Override
    public PageResult<PPWmsTaskDto> queryAll(PPWmsTaskQueryCriteria criteria, Pageable pageable){
        Page<PPWmsTask> page = ppWmsTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppWmsTaskMapper::toDto));
    }

    @Override
    public List<PPWmsTaskDto> queryAll(PPWmsTaskQueryCriteria criteria){
        return ppWmsTaskMapper.toDto(ppWmsTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PPWmsTaskDto findById(Integer id) {
        PPWmsTask ppWmsTask = ppWmsTaskRepository.findById(id).orElseGet(PPWmsTask::new);
        ValidationUtil.isNull(ppWmsTask.getId(),"PpWmsTask","id",id);
        return ppWmsTaskMapper.toDto(ppWmsTask);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PPWmsTask resources) {
        ppWmsTaskRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PPWmsTask resources) {
        PPWmsTask ppWmsTask = ppWmsTaskRepository.findById(resources.getId()).orElseGet(PPWmsTask::new);
        ValidationUtil.isNull( ppWmsTask.getId(),"PpWmsTask","id",resources.getId());
        ppWmsTask.copy(resources);
        ppWmsTaskRepository.save(ppWmsTask);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppWmsTaskRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PPWmsTaskDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PPWmsTaskDto ppWmsTask : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备类型", ppWmsTask.getLogicalAddressType());
            map.put("任务id", ppWmsTask.getWmsTaskId());
            map.put("容器号", ppWmsTask.getLpnCode());
            map.put("容器类型", ppWmsTask.getLpnType());
            map.put("任务类型(1:入库任务    2:出库任务)", ppWmsTask.getWcsTaskType());
            map.put("仓库编码(RTL，PP)", ppWmsTask.getWarehouseCode());
            map.put("巷道号", ppWmsTask.getAisle());
            map.put("取货货位", ppWmsTask.getFromLocation());
            map.put("放货货位", ppWmsTask.getToLocation());
            map.put("优先级", ppWmsTask.getLevel());
            map.put("任务状态", ppWmsTask.getStatus());
            map.put("任务描述", ppWmsTask.getDescription());
            map.put("任务创建时间", ppWmsTask.getCreated());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}